package com.chub.study.config;

import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;

import java.lang.reflect.Field;
import java.util.Properties;

/**
 * @author : chub
 * @version : V1.0.0
 * @fileName : GetSwaggerValue.java
 * @description : TODO
 * @createTime : 2022-03-16 14:57
 */
public class GetSwaggerValue {
    public static void main(String[] args) {
        Properties dingLinkMessageModel = getApiModelProperty("com.chub.study.modules.entity.TCity");
        System.out.println(dingLinkMessageModel);

        // 2.获取类的属性
        // Field[] declaredFields = ReflectUtil.getFields(TCity.class);
    }

    public static Properties getApiModelProperty(String classPath) {
        Properties p = new Properties();
        try {
            // 1.根据类路径获取类
            Class<?> cls = Class.forName(classPath);

            TableName annotationTableName = cls.getAnnotation(TableName.class);
            p.put(annotationTableName.value(), annotationTableName.value());
            ApiModel  annotationApiModel  = cls.getAnnotation(ApiModel.class);
            p.put(annotationApiModel.value(), annotationApiModel.description());

            // 2.获取类的属性
            Field[] declaredFields = cls.getDeclaredFields();
            // 3.遍历属性，获取属性上ApiModelProperty的值，属性的名，存入Properties
            if (declaredFields.length != 0) {
                for (Field field : declaredFields) {
                    // if (field.getAnnotation(ApiModelProperty.class) != null) {
                    //     // key和value可根据需求存
                    //     // 这存的key为注解的值，value为类属性名
                    //     p.put(field.getName(), field.getAnnotation(ApiModelProperty.class).value());
                    // }
                    // if (field.getAnnotation(TableField.class) != null) {
                    //     // key和value可根据需求存
                    //     // 这存的key为注解的值，value为表字段名
                    //     p.put(field.getName(), field.getAnnotation(TableField.class).value());
                    // }
                }
                return p;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
